// 云函数入口文件
const cloud = require('wx-server-sdk')
const mysql = require('mysql2/promise')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  var studentID = event.studentID;
  try {
    const connection = await mysql.createConnection({
      host: "47.115.22.254",
      port: 3306,
      database: "EPMS",
      user: "root",
      password: "1234"
    })
    var studentopenidsql = 'SELECT * FROM openid WHERE studentID = "' + studentID + '";';
    var teacheropenidsql = 'SELECT * FROM teacheropenid WHERE teachersID = "' + studentID + '";';
    var outeropenidsql = 'SELECT * FROM outeropenid WHERE outerpeopleid = "' + studentID + '";';
    var [studentrows, fields] = await connection.execute(studentopenidsql);
    if(studentrows.length == 0){
      var [teacherrows, fields] = await connection.execute(teacheropenidsql);
      if(teacherrows.length == 0){
        var [outerows, fields] = await connection.execute(outeropenidsql);
        return outerows
      }else{
      return teacherrows
    }
    }else {   
    return studentrows
    }
    conection.end();
  } catch (err) {
    return err
  }
}